package com.fbli.test.service.impl;

import com.fbli.test.common.entity.user.UserLoginResMsg;
import com.fbli.test.entity.Authority;
import com.fbli.test.entity.Token;
import com.fbli.test.entity.User;
import com.fbli.test.mapper.UserMapper;
import com.fbli.test.service.AuthorityService;
import com.fbli.test.service.TokenService;
import com.fbli.test.service.UserService;
import com.fbli.test.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    TokenService tokenService;
    @Autowired
    AuthorityService authorityService;
    @Override
    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    @Override
    public User selectByAccount(String account) {
        User user = userMapper.selectByAccount(account);
        return user;
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public UserLoginResMsg login(String account, String password, HttpServletRequest request) {
        User user = userMapper.selectByAccount(account);
        if (user != null && user.getState() && !user.getDeleted()) {
            if (StringUtil.md5Utf8(password).equals(user.getPassword())) {
                String address = StringUtil.getIpAddress(request);
                user.setLoginIP(address);
                user.setLoginTime(System.currentTimeMillis());
                userMapper.updateByPrimaryKey(user);
                UserLoginResMsg msg = new UserLoginResMsg();
                msg.setName(user.getName());
                Token tokenEntity = tokenService.checkToken(user.getId());
                msg.setToken(tokenEntity.getToken());
                msg.setExpireTime(tokenEntity.getExpireTime());
                if(user.getSuperAdmin()){
                    //为管理员添加管理员权限,角色
                    List<Authority> strings = authorityService.selectByUserId(user.getId());
                    if (strings==null){
                        authorityService.addAuthority(new String[]{"admin"},user.getId(),"admin");
                    }
                }else {
                    //为普通用户添加用户权限，角色
                    List<Authority> strings = authorityService.selectByUserId(user.getId());
                    if (strings==null){
                        authorityService.addAuthority(new String[]{"user"},user.getId(),"user");
                    }
                }
                return msg;
            }
        }
        return null;
    }

    @Override
    public boolean logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject == null) {
            return false;
        }
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return false;
        }
        tokenService.refreshToken(user.getId());
        return true;
    }

    @Override
    public boolean addUser(String account, String password) {
        User user = userMapper.selectByAccount(account);
        if (user != null) {
            return false;
        }
        User userEntity = new User();
        userEntity.setAccount(account);
        userEntity.setPassword(StringUtil.md5Utf8(password));
        userEntity.setDeleted(false);
        //初始化生成唯一name
        String name= UUID.randomUUID().toString();
        //替换uuid中的"-"
        name=name.replace("-", "");
        userEntity.setName(name);
        userEntity.setState(true);
        userEntity.setSuperAdmin(false);
        userMapper.insert(userEntity);
        return true;
    }

}
